#include "onionwidget.h"
#include "hsvcolor.h"
#include "ocvtools.h"
#include <QtGui>

#include <cv.h>
#include <highgui.h>

OnionWidget::OnionWidget(QWidget *parent) : QWidget(parent)
{
    m_image = 0;

    int sizeX = 640;
    int sizeY = 480;

    m_background = 0;

    m_chessboard = cvCreateImage(cvSize(sizeX,sizeY), IPL_DEPTH_8U, 3);
    OCVTools::createChessboard(m_chessboard);
}

void OnionWidget::setMaskColor(HSVColor color)
{
    m_maskColor = color;

    processFrame();
}

void OnionWidget::setBackground(QString filename)
{
    m_background = cvLoadImage(filename.toAscii());
    processFrame();
}

void OnionWidget::setShowBackground(bool show)
{
    m_showBackground = show;
    processFrame();
}

void OnionWidget::setChromaKey(bool key)
{
    m_showChromaKey = key;
    processFrame();
}

void OnionWidget::processFrame()
{
    int lines = (int)m_frame->height;
    int rows = (int)m_frame->width;

    if (m_showChromaKey)
    {
        IplImage* composite = cvCreateImage(cvSize(rows,lines), IPL_DEPTH_8U, 3);

        if (m_showBackground && m_background != 0)
        {
            cvCopy(m_background, composite);
        }
        else
        {
            OCVTools::createChessboard(composite);
        }

        //m_image = new QImage((uchar *)composite->imageData, rows, lines, QImage::Format_RGB888);

        IplImage* hsv = cvCreateImage(cvSize(rows,lines), IPL_DEPTH_8U, 3);
        IplImage* mask = cvCreateImage(cvSize(rows,lines), IPL_DEPTH_8U, 1);
        cvSetZero(mask);

        cvCvtColor(m_frame, hsv, CV_BGR2HSV);

        OCVTools::createMask(hsv, mask, m_maskColor);

        cvCopy(m_frame, composite, mask);

        m_image = new QImage((uchar *)composite->imageData, rows, lines, QImage::Format_RGB888);
    }
    else
    {
        m_image = new QImage((uchar *)m_frame->imageData, rows, lines, QImage::Format_RGB888);
    }

    *m_image = m_image->rgbSwapped();

    repaint();
}

void OnionWidget::showFrames(QList<QString> filelist)
{
    qDebug() << "onion show " << filelist;
    m_frame = cvLoadImage(filelist.at(0).toAscii());

    int lines = (int)m_frame->height;
    int rows = (int)m_frame->width;

    if (filelist.size() > 1)
    {
        IplImage* onionframe = 0;

        double alpha = 0.6;
        double beta = 0.3;

        for (int i = 1; i < filelist.size(); ++i)
        {
            onionframe = cvLoadImage(filelist.at(i).toAscii());
            cvAddWeighted(m_frame, alpha, onionframe, beta, 0.0, m_frame);
        }
    }

    processFrame();
}

/*

void OnionWidget::showMasked(QString filename)
{
    int sizeX = 640;
    int sizeY = 480;

    int fieldSize = 20;

    IplImage* chessboard = cvCreateImage(cvSize(sizeX,sizeY), IPL_DEPTH_8U, 3);

    bool dark = true;
    int color = 50;

    int count = 0;

    for (int i=0; i < (sizeX*sizeY)/fieldSize; ++i)
    {
        for (int y=0; y < fieldSize; ++y)
        {
            chessboard->imageData[count*3] = color;
            chessboard->imageData[count*3+1] = color;
            chessboard->imageData[count*3+2] = color;

            count++;
        }

        if (!(count%(sizeX*fieldSize)))
            dark = !dark;

        dark = !dark;
        color = dark ? 90 : 120;
    }

    IplImage* frame = cvLoadImage(filename.toAscii());
    IplImage* hsv = cvCreateImage(cvSize(sizeX,sizeY), IPL_DEPTH_8U, 3);
    IplImage* mask = cvCreateImage(cvSize(sizeX,sizeY), IPL_DEPTH_8U, 1);

    cvCvtColor(frame, hsv, CV_BGR2HSV);

    cvSetZero(mask);
    //OCVTools::maskMatchingPixels(hsv, mask, hsvcolor);
    OCVTools::createMask(hsv, mask, m_maskColor);

    cvCopy(frame, chessboard, mask);

    m_image = new QImage((uchar *)chessboard->imageData, sizeX, sizeY, QImage::Format_RGB888);
    *m_image = m_image->rgbSwapped();

    repaint();
}
*/

void OnionWidget::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        qDebug() << "clicked :" << event->x() << " " << event->y();

        IplImage* hsv = cvCreateImage(cvSize((int)m_frame->width,(int)m_frame->height), IPL_DEPTH_8U, 3);

        cvCvtColor(m_frame, hsv, CV_BGR2HSV);

        HSVColor color = OCVTools::pixelColor(hsv, event->x(), event->y());

        qDebug() << color.h() << color.s() << color.v();

        emit maskColorChoosen(color);

        //emit leftClick(event->x(), event->y());
    }
}

void OnionWidget::paintEvent(QPaintEvent *)
{
    QPainter painter(this);

    if (m_image != 0)
    {
        painter.drawImage(QPointF(0,0), *m_image);
    }
}
